package com.qianli.t263.controller;

import com.qianli.t263.pojo.User;
import com.qianli.t263.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
@SessionAttributes
@Controller
@Service
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 而问题和语句块i来个师傅如果的画面,n.mn
     * 单文件上传
     *5825555dsa5nljkdsabhdsajdsajdbjwahfudsifii7o6hxduhg\]
     * @param user
     * @param session
     * @param request
     * @param attachs
     * @return
     */
    @RequestMapping(value = "/user/useraddsave", method = RequestMethod.POST)
    public String addUserInfo(User user
            , HttpSession session
            , HttpServletRequest request
            , @RequestParam(value = "attachs", required = false) MultipartFile[] attachs) {
        String idPicPath = null;
        String workPicPath = null;
        String errorInfo = null;
        String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
        for (int i = 0; i < attachs.length; i++) {
            MultipartFile attach = attachs[i];
            //判断文件是否为空
            if (!attach.isEmpty()) {
                //原文件名
                String oldFileName = attach.getOriginalFilename();
                //原文件后缀
                String suffix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
                int filesize = 5000000;
                //上传大小不得超过500KB
                if (attach.getSize() > filesize) {
                    request.setAttribute("uploadFileError", "上传文件不得超过500KB");
                    return "useradd";
                    //判断上传文件格式
                } else if (suffix.equalsIgnoreCase("jpg")
                        || suffix.equalsIgnoreCase("png")
                        || suffix.equalsIgnoreCase("jpeg")
                        || suffix.equalsIgnoreCase("pneg")) {

                    String fileName = System.currentTimeMillis() + ((int) (Math.random() * 100000000)) + "_Personal." + suffix;
                    File targetFile = new File(path, fileName);

                    if (!targetFile.exists()) {
                        targetFile.mkdirs();
                    }

                    //保存
                    try {
                        attach.transferTo(targetFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                        request.setAttribute("uploadFileError", "上传失败!");
                        return "useradd";
                    }
                    if (i == 0) {
                        idPicPath = path + File.separator + fileName;
                    } else if (i == 1) {
                        workPicPath = path + File.separator + fileName;
                    }

                    System.out.println("idPicPath>>>>>>>>" + idPicPath);
                } else {
                    request.setAttribute("uploadFileError", "上传图片格式不正确");
                    return "useradd";
                }
            }
        }
        return "";
    }
}
